package com.gientech.consulconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/consumer-test")
    public String test(){

        //通过provider服务名去访问provider
        List<ServiceInstance> instances=this.discoveryClient.getInstances("Provider");
        ServiceInstance serviceInstance=instances.get(0);
        URI url=serviceInstance.getUri();
        String target=url+"/consul-provider";

        String rst=restTemplate.getForObject(target,String.class);
        return "consumer:"+rst;
    }
}
